<!-- getchecklist.php
     A PHP script to access the advising database through MySQL
     -->
<html>
<head>
<title> get checklist </title>
</head>
<body>

<?php

// Connect to MySQL  

$db = mysql_connect("localhost", "root", "");
if (!$db) {
     print "Error - Could not connect to MySQL";
     exit;
}

// Select the database

$er = mysql_select_db("db4208");
if (!$er) {
    print "Error - Could not select the cars database";
    exit;
}

// Get the query and clean it up (delete leading and trailing 
// whitespace and remove backslashes from magic_quotes_gpc)

$param = $_POST['sid'];
trim($param);
$param = stripslashes($param);

// Get information of non-6990 courses 
// step 1: construct the SQL statement  
$stmt = 'SELECT C.major, O.coursenum, C.title, O.year, O.semester, C.area_1, ' 
	.  'T.grade, T.count_to '  
	.  'FROM takes T JOIN offer O ON(T.offerid=O.offerid) '
	.  'JOIN course C ON(O.major=C.major AND O.coursenum=C.coursenum) ' 
	.  'WHERE O.coursenum <> 6990 AND T.studentID=' . $param ; 

// step 2: execute the SQL statement 
$result = mysql_query($stmt);
if (!$result) {
    print "<p> Error - the first query could not be executed </p>"; 
    print "<p> param=" . $param . "</p>"; 
    print "<p> the SQL statement: " . $stmt. "</p>"; 
    $error = mysql_error();
    print "<p>" . $error . "</p>";
    exit;
} 

// Get the number of rows in the result, and the first row
$num_rows = mysql_num_rows($result);

// Display the results in a table
print "<table><caption> <h2> Query Results </h2> </caption> <tbody>";
// Output the values of the fields in the rows 
while ($row = mysql_fetch_array($result)) { 
    $values = array_values($row); 
    print "<tr>";
    
    echo"<td><input type='text' disabled='disabled' size = '6' name='dept' value='".$values[0]."'></td>";
	echo"<td><input type='text' disabled='disabled' size = '6' name='number' value='".$values[2]."'></td>";
	echo"<td><input type='text' disabled='disabled' name='name' value='".$values[4]."'></td>";
    echo"<td><input type='text' disabled='disabled' size = '6' name='year' value='".$values[6]."'></td>";
    echo"<td><input type='text' disabled='disabled' size = '6' name='sem' value='".$values[8]."'></td>";
    echo"<td><input type='text'  size = '4' name='area' value='".$values[10]."'></td>";
    echo"<td><input type='text' disabled='disabled' size = '4' name='grade' value='".$values[12]."'></td>";
    echo"<td><input type='text' disabled='disabled' size = '4'name='count' value='".$values[14]."'></td>";
    
    echo "</tr>";
} 

// Get information of 6990 courses 
// step 1: construct the SQL statement  
$stmt6990 = 'SELECT C.major, O.coursenum, O.title, O.year, O.semester, O.area_1, ' 
	.  'T.grade, T.count_to '  
	.  'FROM takes T JOIN offer O ON(T.offerid=O.offerid) '
	.  'JOIN course C ON(O.major=C.major AND O.coursenum=C.coursenum) ' 
	.  'WHERE O.coursenum=6990 AND T.studentID=' . $param ; 

// step 2: execute the SQL statement 
$result = mysql_query($stmt6990);
if (!$result) {
    print "Error - the second query could not be executed"; 
    $error = mysql_error();
    print "<p>" . $error . "</p>";
    exit;
} 
// Get the number of rows in the result, and the first row


// Output the values of the fields in the rows 
while ($row = mysql_fetch_array($result)) { 
    $values = array_values($row); 
    print "<tr>";
    echo"<td><input type='text' disabled='disabled' size = '6' name='dept' value='".$values[0]."'></td>";
	echo"<td><input type='text' disabled='disabled' size = '6' name='number' value='".$values[2]."'></td>";
	echo"<td><input type='text' disabled='disabled' name='name' value='".$values[4]."'></td>";
    echo"<td><input type='text' disabled='disabled' size = '6' name='year' value='".$values[6]."'></td>";
    echo"<td><input type='text' disabled='disabled' size = '6' name='sem' value='".$values[8]."'></td>";
    echo"<td><input type='text'  size = '4' name='area' value='".$values[10]."'></td>";
    echo"<td><input type='text' disabled='disabled' size = '4' name='grade' value='".$values[12]."'></td>";
    echo"<td><input type='text' disabled='disabled' size = '4' name='count' value='".$values[14]."'></td>";
    
    echo "</tr>";
} 

print "</tbody></table>";
?>
</body>
</html>